package cn.com.dragonpass.psystem.common.util;
import cn.com.dragonpass.pmodel.settle.center.currency.ExchageRateExcleDto;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.handler.RowWriteHandler;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.WriteTable;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.excel.write.style.column.AbstractColumnWidthStyleStrategy;
import com.alibaba.excel.write.style.column.LongestMatchColumnWidthStyleStrategy;
import com.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Test3 {


    public static class CustomRowWriteHandler implements RowWriteHandler {



        private int firstRow;
        private int lastRow;
        private int firstCol;
        private int lastCol;


        public CustomRowWriteHandler(int firstRow, int lastRow, int firstCol, int lastCol) {
            this.firstRow = firstRow;
            this.lastRow = lastRow;
            this.firstCol = firstCol;
            this.lastCol = lastCol;
        }

        @Override
        public void beforeRowCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Integer integer, Integer integer1, Boolean aBoolean) {
            //   System.out.println("..............");
        }

        @Override
        public void afterRowCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Integer integer, Boolean aBoolean) {
            // System.out.println("..............*********");
        }

        @Override
        public void afterRowDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row,
                                    Integer relativeRowIndex, Boolean isHead) {

            //  System.out.println(relativeRowIndex+"------->"+row.getRowNum());
            // 在这里编写你的自定义逻辑
            // 例如，给每一行添加一个公共的背景颜色
            // row.getCell(0).getCellStyle().setFillForegroundColor(IndexedColors.YELLOW.getIndex());
            // row.getCell(0).getCellStyle().setFillPattern(FillPatternType.SOLID_FOREGROUND);


            if ( relativeRowIndex.equals(firstRow)&&row.getRowNum()==0) {
                CellRangeAddress cellRangeAddress = new CellRangeAddress(firstRow, lastRow, firstCol, lastCol);
                writeSheetHolder.getSheet().addMergedRegion(cellRangeAddress);



            }
            if (relativeRowIndex == 0 &&row.getRowNum()==0) {
                row.setHeight((short) 2500);




            }else{
                Cell cell = row.getCell(0);
                CellStyle cellStyle = cell.getCellStyle();
                cellStyle.setWrapText(true);
                cellStyle.setBorderRight(BorderStyle.THIN);
                cellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
                cell.setCellStyle(cellStyle);
            }
        }
    }
    private static  List<List<String>> head() {
        List<List<String>> list = new ArrayList<List<String>>();
        List<String> head0 = new ArrayList<String>();
        String msg="1、导入字段说明\n" +
                "（1）币种编码：必填，需大写，如CNY、USD；\n" +
                "（2）币种名称：如人民币、美元等；\n" +
                "（3）汇率：必填，支持小数点后四位，不足四位补0，多余四位采用四舍五入；\n" +
                "2、若对应币种汇率无需更新，模板需要删除该币种数据；\n" +
                "3、以人民币为本位币导入汇率数据；";
        head0.add(msg);
        List<String> head1 = new ArrayList<String>();
        head1.add("数字" );
        List<String> head2 = new ArrayList<String>();
        head2.add("日期");
        list.add(head0);
        list.add(head1);
        list.add(head2);
        return list;
    }

    public static void main(String[] args) {
        String fileName = "D:\\example4.xlsx";
        // 头的策略
        WriteCellStyle headWriteCellStyle = new WriteCellStyle();
        // 背景设置为红色
        //headWriteCellStyle.setFillForegroundColor(IndexedColors.RED.getIndex());
        WriteFont headWriteFont = new WriteFont();
        headWriteFont.setFontHeightInPoints((short)10);
        headWriteCellStyle.setWriteFont(headWriteFont);
        // 内容的策略
        WriteCellStyle contentWriteCellStyle = new WriteCellStyle();
        // 这里需要指定 FillPatternType 为FillPatternType.SOLID_FOREGROUND 不然无法显示背景颜色.头默认了 FillPatternType所以可以不指定
        //contentWriteCellStyle.setFillPatternType(FillPatternType.SOLID_FOREGROUND);
        // 背景绿色
      //  contentWriteCellStyle.setFillForegroundColor(IndexedColors.GREEN.getIndex());
        WriteFont contentWriteFont = new WriteFont();
        // 字体大小
        contentWriteFont.setFontHeightInPoints((short)12);
        contentWriteCellStyle.setWriteFont(contentWriteFont);

        contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.TOP);
        contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);
        contentWriteCellStyle.setBorderLeft(BorderStyle.THIN);
        contentWriteCellStyle.setBorderRight(BorderStyle.THIN);
        contentWriteCellStyle.setBorderTop(BorderStyle.THIN);
        contentWriteCellStyle.setBorderBottom(BorderStyle.THIN);
        contentWriteCellStyle.setTopBorderColor(IndexedColors.BLACK.getIndex());
        contentWriteCellStyle.setRightBorderColor(IndexedColors.BLACK.getIndex());
        // 这个策略是 头是头的样式 内容是内容的样式 其他的策略可以自己实现
        HorizontalCellStyleStrategy horizontalCellStyleStrategy =
                new HorizontalCellStyleStrategy(headWriteCellStyle, contentWriteCellStyle);

        // 这里 需要指定写用哪个class去写，然后写到第一个sheet，名字为模板 然后文件流会自动关闭
        EasyExcel.write(fileName).head(head())
                .registerWriteHandler(horizontalCellStyleStrategy).registerWriteHandler(new TestExcle.CustomRowWriteHandler(0,0,0,2)).registerWriteHandler(new SimpleColumnWidthStyleStrategy(30))
                .sheet("模板")
                .doWrite(getData());


    }

    private static List<List<String>> getData() {
        List<List<String>> data = new ArrayList<>();
        data.add(Arrays.asList("币种编码" ,"币种名称 ","汇率 "));
        data.add(Arrays.asList("xxssss，。","yyyyyyysss","xxsssss "));
        return data;
    }
}
